﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
#include <QMessageBox>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{

    this->op=new fileOperator;
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}
//判断账号密码是否正确
bool MainWindow::isYouself(QString id,QString password)
{
    for(int i=0;i<this->infoLine.length();i++)
    {
        QString line=infoLine.at(i);
        line=line.trimmed();
        QStringList linesplit=line.split(",");
        if(id==linesplit.at(1) && password==linesplit.at(2))
        {
            return true;
        }
    }
        return false;
}

void MainWindow::on_btn_cancel_clicked()
{
    this->close();
}

//登录按钮 按了登录按钮后执行这个函数
void MainWindow::on_btn_logIn_clicked()
{
    //把页面里用户输入的id和密码传给两个参数
    QString id=this->ui->loginId->text();
    QString password=this->ui->loginPassword->text();
    if(this->ui->btn_student->isChecked())//用于确定是否选中了student的复选框。即CheckBox对象。区分CheckBox是否被选中
    {
        this->op->readFromInfoFile("stuInfo.csv",infoLine);
        for(int i=0;i<this->infoLine.length();i++)
        {
            QString line=infoLine.at(i);
            line=line.trimmed();
            //qDebug<<"student"<<endl;
            QStringList linesplit=line.split(",");
        }
        if(this->isYouself(id,password))
        {
            this->stuview=new studentMenu; //新建studentMenu学生菜单 要看看这里面有哪些函数 这些函数都是干什么的
            this->stuview->getLoginId(id); //把检查成功的id丢给getLoginId函数 作为stuview这个对象的一个成员变量
            this->stuview->show();
            this->stuview->setStuObject();//
            this->close();
        }
        else
        {
            QMessageBox::critical(this,"错误！","用户名或密码错误！","确定");
        }
    }
    else if(this->ui->btn_teacher->isChecked())
    {
        this->op->readFromInfoFile("teacherInfo.csv",infoLine);
        if(this->isYouself(id,password))
        {
            this->teaview=new teacherMenu; //查看teacher的菜单
            this->teaview->getLoginId(id);
            this->teaview->show();
            this->teaview->setTeaObject();
            this->close();
        }
        else
        {
            QMessageBox::critical(this,"错误！","用户名或密码错误！","确定");
        }
    }
    else if(this->ui->btn_admin->isChecked())
    {
        this->op->readFromInfoFile("adminInfo.csv",infoLine);
        if(this->isYouself(id,password))
        {
            this->adminview=new adminMenu;
            this->adminview->getLoginId(id);
            this->adminview->setAdminObject();
            this->adminview->show();
            this->close();
        }
        else
        {
            QMessageBox::critical(this,"错误！","用户名或密码错误！","确定");
        }
    }
    else
    {
        QMessageBox::critical(this,"错误！","请选择角色！","确定");
    }
}

